package application;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Main extends Application {

    Logger logger = LoggerFactory.getLogger(Main.class);

    Stage primaryStage;

    @FXML
    TextField filepath;
    @FXML
    TextField ip_;
    @FXML
    TextField port_;

    @FXML
    TableView<Bean> table;

    @FXML
    TableColumn<Bean, String> index;
    @FXML
    TableColumn<Bean, String> ip;
    @FXML
    TableColumn<Bean, String> port;
    @FXML
    TableColumn<Bean, String> status;
    @FXML
    TableColumn<Bean, String> rate;

    ExecutorService executorService = Executors.newFixedThreadPool(16);

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("main.fxml")));
            Scene scene = new Scene(root, 388, 375);
            scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("application.css")).toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);

            this.primaryStage = primaryStage;
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void filePicker() {

        index.setCellValueFactory(new PropertyValueFactory<>("index"));
        ip.setCellValueFactory(new PropertyValueFactory<>("ip"));
        port.setCellValueFactory(new PropertyValueFactory<>("port"));
        status.setCellValueFactory(new PropertyValueFactory<>("status"));
        rate.setCellValueFactory(new PropertyValueFactory<>("rate"));

        FileChooser fc = new FileChooser();
        File file = fc.showOpenDialog(primaryStage);
        if (file != null) {
            filepath.setText(file.getAbsolutePath());
        }
    }

    public void start() {
        ObservableList<Bean> observableList = FXCollections.observableArrayList();
        List<Bean> list = new FileScan().fileScan(filepath.getText());
        Operator operator = new Operator();
        if (!list.isEmpty()) {
            for (int index = 0; index < list.size(); index++) {
                Bean bean = list.get(index);
                bean.setIndex(index + 1);
                invokePing(observableList, operator, bean);
            }
        } else {
            Bean bean = new Bean(1, ip_.getText(), port_.getText(), null, null);
            invokePing(observableList, operator, bean);
        }
    }

    private void invokePing(ObservableList<Bean> observableList, Operator operator, Bean bean) {
        observableList.add(bean);
        table.setItems(observableList);
        executorService.submit(() -> {
            operator.pingStart(bean);
            table.refresh();
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
